<template>
  <div class="homework-container">
    <h1>发布作业</h1>
    
    <div class="user-info">
      <p>欢迎，{{ userProfile.fullName }}老师</p>
      <router-link to="/teacher/dashboard">返回Dashboard</router-link>
    </div>

    <div class="teaching-courses" v-if="userProfile.teachingCourses && userProfile.teachingCourses.length > 0">
      <h2>选择班级和学科发布作业</h2>
      <div class="course-selection">
        <div 
          v-for="(course, index) in userProfile.teachingCourses" 
          :key="index" 
          class="course-card"
          :class="{ selected: selectedCourse === course }"
          @click="selectCourse(course)"
        >
          <p>班级: {{ course.className }}</p>
          <p>科目: {{ course.subjectName }}</p>
        </div>
      </div>
    </div>

    <div v-else>
      <p>您还没有授课信息</p>
    </div>

    <!-- 作业发布表单 -->
    <div v-if="selectedCourse" class="homework-form">
      <h2>发布作业到 {{ selectedCourse.className }} - {{ selectedCourse.subjectName }}</h2>
      <form @submit.prevent="submitHomework">
        <div class="form-group">
          <label for="title">作业标题:</label>
          <input 
            type="text" 
            id="title" 
            v-model="homeworkForm.title" 
            required
            placeholder="请输入作业标题"
          />
        </div>
        
        <div class="form-group">
          <label for="content">作业内容:</label>
          <textarea 
            id="content" 
            v-model="homeworkForm.content" 
            required
            placeholder="请输入作业内容"
            rows="5"
          ></textarea>
        </div>
        
        <div class="form-group">
          <label for="dueDate">截止日期:</label>
          <input 
            type="datetime-local"
            id="dueDate" 
            v-model="homeworkForm.dueDate"
          />
        </div>
        
        <div class="form-actions">
          <button type="submit" class="submit-btn">发布作业</button>
          <button type="button" class="cancel-btn" @click="resetForm">取消</button>
        </div>
      </form>
    </div>

    <!-- 成功提示 -->
    <div v-if="successMessage" class="success-message">
      {{ successMessage }}
    </div>

    <!-- 错误提示 -->
    <div v-if="errorMessage" class="error-message">
      {{ errorMessage }}
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';

export default {
  name: 'AssignHomework',
  setup() {
    const router = useRouter();
    const userProfile = ref({});
    const selectedCourse = ref(null);
    const homeworkForm = ref({
      title: '',
      content: '',
      dueDate: '',
      classId: null,
      subjectId: null
    });
    
    const successMessage = ref('');
    const errorMessage = ref('');

    // 获取用户信息
    const loadUserProfile = async () => {
      try {
        const response = await fetch('/user/profile', {
          method: 'GET',
          credentials: 'include',
          headers: {
            'Content-Type': 'application/json',
            'Cache-Control': 'no-cache'
          }
        });
        
        const result = await response.json();
        if (result.code === 1) {
          userProfile.value = result.data;
        } else {
          errorMessage.value = result.msg || '获取用户信息失败';
        }
      } catch (err) {
        errorMessage.value = '网络错误，请稍后重试';
      }
    };

    // 选择课程
    const selectCourse = (course) => {
      selectedCourse.value = course;
      homeworkForm.value.classId = course.classId;
      homeworkForm.value.subjectId = course.subjectId;
    };

    // 重置表单
    const resetForm = () => {
      selectedCourse.value = null;
      homeworkForm.value = {
        title: '',
        content: '',
        dueDate: '',
        classId: null,
        subjectId: null
      };
      successMessage.value = '';
      errorMessage.value = '';
    };

    // 提交作业
    const submitHomework = async () => {
      try {
        // 清除之前的消息
        successMessage.value = '';
        errorMessage.value = '';
        
        const response = await fetch('/homework/assign', {
          method: 'POST',
          credentials: 'include',
          headers: {
            'Content-Type': 'application/json'
          },
          body: JSON.stringify(homeworkForm.value)
        });
        
        const result = await response.json();
        if (result.code === 1) {
          successMessage.value = result.data || '作业发布成功';
          // 重置表单
          resetForm();
        } else {
          errorMessage.value = result.msg || '作业发布失败';
        }
      } catch (err) {
        errorMessage.value = '网络错误，请稍后重试';
      }
    };

    onMounted(() => {
      loadUserProfile();
    });

    return {
      userProfile,
      selectedCourse,
      homeworkForm,
      successMessage,
      errorMessage,
      selectCourse,
      resetForm,
      submitHomework
    };
  }
};
</script>

<style scoped>
.homework-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
  font-family: Arial, sans-serif;
}

.homework-container h1 {
  text-align: center;
  color: #333;
}

.user-info {
  background-color: #f5f5f5;
  padding: 15px;
  border-radius: 5px;
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.user-info a {
  color: #409EFF;
  text-decoration: none;
}

.user-info a:hover {
  text-decoration: underline;
}

.teaching-courses h2 {
  color: #333;
}

.course-selection {
  display: flex;
  flex-wrap: wrap;
  gap: 15px;
}

.course-card {
  background-color: #fff;
  border: 1px solid #ddd;
  border-radius: 5px;
  padding: 15px;
  cursor: pointer;
  width: calc(50% - 15px);
  box-sizing: border-box;
}

.course-card:hover {
  border-color: #409EFF;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.course-card.selected {
  border-color: #409EFF;
  background-color: #e8f4fd;
  box-shadow: 0 2px 8px rgba(64, 158, 255, 0.2);
}

.homework-form {
  background-color: #fff;
  border: 1px solid #ddd;
  border-radius: 5px;
  padding: 20px;
  margin-top: 20px;
}

.homework-form h2 {
  margin-top: 0;
  color: #333;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 5px;
  font-weight: bold;
  color: #555;
}

.form-group input,
.form-group textarea {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 16px;
  box-sizing: border-box;
}

.form-group input:focus,
.form-group textarea:focus {
  border-color: #409EFF;
  outline: none;
  box-shadow: 0 0 5px rgba(64, 158, 255, 0.3);
}

.form-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-start;
}

.submit-btn, .cancel-btn {
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
}

.submit-btn {
  background-color: #67C23A;
  color: white;
}

.submit-btn:hover {
  background-color: #55a52a;
}

.cancel-btn {
  background-color: #9E9E9E;
  color: white;
}

.cancel-btn:hover {
  background-color: #757575;
}

.success-message {
  background-color: #d4edda;
  color: #155724;
  padding: 15px;
  border-radius: 5px;
  margin-top: 20px;
}

.error-message {
  background-color: #f8d7da;
  color: #721c24;
  padding: 15px;
  border-radius: 5px;
  margin-top: 20px;
}

@media (max-width: 600px) {
  .course-card {
    width: 100%;
  }
  
  .form-actions {
    flex-direction: column;
  }
}
</style>